﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;


namespace WLC
{
    public class WLCWorkstationListElementDefinition : IElementDefinition
    {
        #region IElementDefinition Members

        public IElement CreateElement(IElementData data)
        {
            return new WLCWorkstationListElement(data);
        }


        public void DefineSchema(IElementSchema schema)
        {
            IPropertyDefinitions propSchema = schema.PropertyDefinitions;
            IPropertyDefinitions repGroupProp = propSchema.AddRepeatGroup("WorkstationList", "The repeated property group containing all workstations and their relevant information.");

            IPropertyDefinition workstationName = repGroupProp.AddStringProperty("WorkstationName", String.Empty);
            workstationName.Description = "The name of the workstation object in the model.";
            workstationName.Required = true;


            IPropertyDefinition currentWorkload = repGroupProp.AddStateProperty("CurrentWorkload");
            currentWorkload.Description = "The state variable that contains the current total workload of the workstation.";
            currentWorkload.Required = true;

            IPropertyDefinition workloadLimit = repGroupProp.AddStateProperty("WorkloadLimit");
            workloadLimit.Description = "The state variable that contains the workload limit for the workstation.";
            workloadLimit.Required = true;

            IPropertyDefinition loadPercentage = repGroupProp.AddStateProperty("LoadPercentage");
            loadPercentage.Description = "The state variable that contains the load percentage for this workstation.";
            loadPercentage.Required = true;

            IStateDefinition numberOfWorkstations = schema.StateDefinitions.AddState("NumberOfWorkstations");
            numberOfWorkstations.Description = "Holds the number of workstations in this list.";
            
        }


        public string Name
        {
            get { return "WLCWorkstationList"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }


        
        public static readonly Guid ID = new Guid("{3F254790-060A-11DF-A2E7-DC7D56D89593}");

        public Guid UniqueID
        {
            get { return ID; }
        }

        #endregion
    }


    public class WLCWorkstationListElement : IElement
    {
        IElementData _data;
        int _numOfworkstations;
        IState _numberOfWorkstationsState;

        public WLCWorkstationListElement(IElementData data)
        {
            _data = data;
            _numOfworkstations = ((IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList")).GetCount(_data.ExecutionContext);
            _numberOfWorkstationsState = data.States["NumberOfWorkstations"];
            _numberOfWorkstationsState.StateValue = _numOfworkstations;
          
        }



        #region IElement Members

        public double GetLoadLimitForWorkstation(String name)
        {
            double result = 0;

            IRepeatingPropertyReader workstationsProp = (IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList");

            for (int i = 0; i < _numOfworkstations; i++)
            {
                if (workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("WorkstationName").GetStringValue(_data.ExecutionContext).Equals(name))
                {
                    result = ((IStateProperty)workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("WorkloadLimit")).GetState(_data.ExecutionContext).StateValue;
                    break;
                }


            }


            return result;

        }

        public double AddToLoadLimitForWorkstation(String name, double workload)
        {
            double result = 0;


            IRepeatingPropertyReader workstationsProp = (IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList");

            for (int i = 0; i < _numOfworkstations; i++)
            {
                if (workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("WorkstationName").GetStringValue(_data.ExecutionContext).Equals(name))
                {
                    ((IStateProperty)workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("CurrentWorkload")).GetState(_data.ExecutionContext).StateValue += workload;
                    result = ((IStateProperty)workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("CurrentWorkload")).GetState(_data.ExecutionContext).StateValue;
                    break;
                }


            }


            return result;
        }

        public double SetLoadForWorkstation(String name, double workload)
        {
            double result = 0;


            IRepeatingPropertyReader workstationsProp = (IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList");

            for (int i = 0; i < _numOfworkstations; i++)
            {
                if (workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("WorkstationName").GetStringValue(_data.ExecutionContext).Equals(name))
                {
                    ((IStateProperty)workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("CurrentWorkload")).GetState(_data.ExecutionContext).StateValue = workload;
                    result = ((IStateProperty)workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("CurrentWorkload")).GetState(_data.ExecutionContext).StateValue;
                    break;
                }


            }


            return result;
        }


        public double GetLoadForWorkstation(String name)
        {
            double result = 0;

            IRepeatingPropertyReader workstationsProp = (IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList");

            for (int i = 0; i < _numOfworkstations; i++)
            {
                if (workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("WorkstationName").GetStringValue(_data.ExecutionContext).Equals(name))
                {
                    result = ((IStateProperty)workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("CurrentWorkload")).GetState(_data.ExecutionContext).StateValue;
                    break;
                }


            }


            return result;


        }


        public double GetLoadPercentageForWorkstation(String name)
        {
            double result = 0;

            IRepeatingPropertyReader workstationsProp = (IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList");

            for (int i = 0; i < _numOfworkstations; i++)
            {
                if (workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("WorkstationName").GetStringValue(_data.ExecutionContext).Equals(name))
                {
                    result = ((IStateProperty)workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("LoadPercentage")).GetState(_data.ExecutionContext).StateValue;
                    break;
                }


            }


            return result;


        }

        public List<string> GetWorkstationNames()
        {
            List<string> list = new List<string>();
            IRepeatingPropertyReader workstationsProp = (IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList");

            for (int i = 0; i < _numOfworkstations; i++)
            {
                list.Add(workstationsProp.GetRow(i, _data.ExecutionContext).GetProperty("WorkstationName").GetStringValue(_data.ExecutionContext));

            }
            return list;
        }


        public void Initialize()
        {
            _numOfworkstations = ((IRepeatingPropertyReader)_data.Properties.GetProperty("WorkstationList")).GetCount(_data.ExecutionContext);
            _numberOfWorkstationsState = _data.States["NumberOfWorkstations"];
            _numberOfWorkstationsState.StateValue = _numOfworkstations;

        }

        public void Shutdown()
        {
            // No shutdown code necessary
        }

        #endregion
    }



}

